package club.framework.web.application.sys.user.controller;

import club.framework.web.application.sys.user.entity.User;
import club.framework.web.application.sys.user.service.IUserService;
import club.framework.web.base.entity.BasePageReqEntity;
import club.framework.web.base.entity.BasePageResultEntity;
import club.framework.web.base.entity.Result;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;


@Controller
@RequestMapping("sys/user")
public class UserController {

    private static  final Logger log = LogManager.getLogger(UserController.class);

    @Resource
    private IUserService userService;

    @RequestMapping("index")
    public String   index(){
        return "application/sys/user/index";
    }

    @RequestMapping(value = "loadpage",method = RequestMethod.POST)
    @ResponseBody
    public Object loadPage(@RequestBody BasePageReqEntity<User> basePageReqEntity){
        BasePageResultEntity<User> basePageResultEntity =userService.queryPage(basePageReqEntity);
        return basePageResultEntity;
    }

    @RequestMapping(value = "add",method = RequestMethod.POST)
    @ResponseBody
    public Object add(User user){
        Result result = new Result();
        try{
            userService.save(user);
        }catch (Exception ex){
            log.error("新增用户:{},失败: {}",user,ex.getMessage());
            result.setCode(Result.ERROR_CODE);
            result.setMsg(ex.getMessage());
        }
        return result;
    }

    @RequestMapping(value="update",method = RequestMethod.POST)
    @ResponseBody
    public Object update(User user){
        Result result = new Result();
        try{
            userService.update(user);
        }catch (Exception ex){
            log.error("用户：{},更新失败: {}",user,ex.getMessage());
            result.setCode(Result.ERROR_CODE);
            result.setMsg(ex.getMessage());
        }
        return result;
    }

    @RequestMapping("view")
    public Object view(int id){
        return null;
    }

    @RequestMapping("deletebyid")
    @ResponseBody
    public  Object deleteById(int id){
        Result result = new Result();
        try{
            userService.delete(id);
        }catch (Exception ex){
            log.error("根据id:{},删除用户信息失败: {}",id,ex.getMessage());
            result.setCode(Result.ERROR_CODE);
            result.setMsg(ex.getMessage());
        }
        return result;
    }


    @RequestMapping("getuserbyid")
    @ResponseBody
    public Object getUserById(int id){
        Result result = new Result();
        try{
            result.setData(userService.getObject(id));
        }catch (Exception ex){
            log.error("根据id:{},获取用户信息失败: {}",id,ex.getMessage());
            result.setCode(Result.ERROR_CODE);
            result.setMsg(ex.getMessage());
        }
        return result;
    }

    @RequestMapping("treemenu")
    public String treeMenu(int id, ModelMap model){
        model.put("userId",id);
        return "application/sys/user/treemenu";
    }

    @RequestMapping("saveusermenu")
    @ResponseBody
    public Object saveUserMenu(@RequestParam("userid") int userId,@RequestParam("menuids") String menuIds){
        Result result = new Result();
        try {
            userService.saveUserMenu(userId,menuIds);
        }catch (Exception ex){
            log.error("根据用户id:{},menuids:{},保存信息失败:{}",userId,menuIds,ex.getMessage());
            result.setCode(Result.ERROR_CODE);
            result.setMsg(ex.getMessage());
        }
        return result;
    }


}
